/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.monitor.videodetection.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.monitor.videodetection.entity.MonitorVideoDetectionMain;
import com.jeeplus.modules.monitor.videodetection.service.MonitorVideoDetectionMainService;

/**
 * 视频检测Controller
 * @author 刘明星
 * @version 2020-12-11
 */
@RestController
@RequestMapping(value = "/monitor/videodetection/monitorVideoDetectionMain")
public class MonitorVideoDetectionMainController extends BaseController {

	@Autowired
	private MonitorVideoDetectionMainService monitorVideoDetectionMainService;

	@ModelAttribute
	public MonitorVideoDetectionMain get(@RequestParam(required=false) String id) {
		MonitorVideoDetectionMain entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = monitorVideoDetectionMainService.get(id);
		}
		if (entity == null){
			entity = new MonitorVideoDetectionMain();
		}
		return entity;
	}


	/**
	 * 视频检测树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(MonitorVideoDetectionMain monitorVideoDetectionMain) {
		return AjaxJson.success().put("list", monitorVideoDetectionMainService.findList(monitorVideoDetectionMain));
	}

	/**
	 * 根据Id获取视频检测数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(MonitorVideoDetectionMain monitorVideoDetectionMain) {
		return AjaxJson.success().put("monitorVideoDetectionMain", monitorVideoDetectionMain);
	}

	/**
	 * 保存视频检测
	 */
	@PostMapping("save")
	public AjaxJson save(MonitorVideoDetectionMain monitorVideoDetectionMain, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(monitorVideoDetectionMain);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		monitorVideoDetectionMainService.save(monitorVideoDetectionMain);//保存
		return AjaxJson.success("保存视频检测成功");
	}

	/**
	 * 删除视频检测
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(MonitorVideoDetectionMain monitorVideoDetectionMain) {
		monitorVideoDetectionMainService.delete(monitorVideoDetectionMain);
		return AjaxJson.success("删除视频检测成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<MonitorVideoDetectionMain> list = monitorVideoDetectionMainService.findList(new MonitorVideoDetectionMain());
		List rootTree = getMonitorVideoDetectionMainTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<MonitorVideoDetectionMain> getMonitorVideoDetectionMainTree(List<MonitorVideoDetectionMain> list, String extId) {
		List<MonitorVideoDetectionMain> monitorVideoDetectionMains = Lists.newArrayList();
		List<MonitorVideoDetectionMain> rootTrees = monitorVideoDetectionMainService.getChildren("0");
		for (MonitorVideoDetectionMain root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        monitorVideoDetectionMains.add(getChildOfTree(root, list, extId));
		    }
		}
		return monitorVideoDetectionMains;
	}

	private MonitorVideoDetectionMain getChildOfTree(MonitorVideoDetectionMain monitorVideoDetectionMain, List<MonitorVideoDetectionMain> monitorVideoDetectionMainList, String extId) {
		monitorVideoDetectionMain.setChildren(Lists.newArrayList());
		for (MonitorVideoDetectionMain child : monitorVideoDetectionMainList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(monitorVideoDetectionMain.getId())) {
		            monitorVideoDetectionMain.getChildren().add(getChildOfTree(child, monitorVideoDetectionMainList, extId));
		        }
		    }
		}
		return monitorVideoDetectionMain;
	}



}